// 实验状态
// todo
// []m3u8 绝对地址
// []m3u8 相对地址
// []ts 绝对地址
// []ts 相对地址
var M3U8Downloader = (function () {
    async function main(m3u8Url, name) {
        let urlArray = await decode(m3u8Url);
        await DownloaderCore(urlArray.flat(), name);
        console.log("%c 100%下载完成", "color:green");
    }
    async function decode(url) {
        let res = await fetch(url).then((res) => res.text());
        let b = res.match(/^(http).*?\.m3u8/gim);
        let d = res.match(/.*?\.m3u8/gim);
        let c = res.match(/^(http).*?\.ts/gim);
        let a = res.match(/.*?\.ts/gim);
        if (b) {
            return b.map((i) => decode[i]).flat();
        } else if (d) {
            let allm3u8 = d.map((i) => {
                return url.replace(/(?<=\/)[^\/]*?\.m3u8/gi, "") + i;
            });
            return allm3u8.map((i) => decode[i]).flat();
        } else if (c) {
            return c;
        } else if (a) {
            let all = a.map((i) => {
                return url.replace(/(?<=\/)[^\/]*?\.m3u8/gi, "") + i;
            });
            return all;
        }
    }
    async function DownloaderCore(urlArray, name) {
        let num = urlArray.length;
        let result = [];
        for (let i = num; i--; ) {
            let res = await fetch(urlArray[i]).then((res) => res.blob());
            result.push(res);
        }
        let blob = new Blob(result);
        let url = URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.href = url;
        a.download = name;
        a.click();
    }
    return { get: main };
})();
